package com.server.llh.llhserverwork.activity.server;


import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.family.llh.lelaohuitoastview.dialog.toast.T;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lelaohui.net.BaseLeLaohuiAcitivity;
import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.ServerNetRequestManager;
import com.lelaohui.net.request.LRequest;
import com.lelaohui.net.utils.UrlsTools;
import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.adapter.DetailListAdapter;
import com.server.llh.llhserverwork.adapter.LlhAddressItemAdapter;
import com.server.llh.llhserverwork.adapter.ServerInfoAdapter;
import com.server.llh.llhserverwork.bean.AddressManagerBean;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.bean.MyAddress;
import com.server.llh.llhserverwork.bean.OldManInfo;
import com.server.llh.llhserverwork.bean.OrderBean;
import com.server.llh.llhserverwork.bean.SerStockDetailWapBean;
import com.server.llh.llhserverwork.bean.ServerInfoBean;
import com.server.llh.llhserverwork.bean.SubscribeOrderBean;
import com.server.llh.llhserverwork.port.QuitCloseIntetface;
import com.server.llh.llhserverwork.port.SubscribeInterface;
import com.server.llh.llhserverwork.utils.ListViewHeightUtil;
import com.server.llh.llhserverwork.utils.ServerPort;
import com.server.llh.llhserverwork.utils.SharedPreferenceUtil;
import com.server.llh.llhserverwork.utils.TimeShowUtils;
import com.server.llh.llhserverwork.utils.TimeUtil;
import com.server.llh.llhserverwork.utils.ToastTool;
import com.server.llh.llhserverwork.view.AddListView;
import com.server.llh.llhserverwork.view.CheckableLinearLayout;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 服务预约
 */
public class LlhSubscribeActivity extends BaseLeLaohuiAcitivity implements SubscribeInterface {
    private static String TAG = "LlhSubscribe";
    @BindView(R.id.left_btn)
    ImageButton leftBtn;
    @BindView(R.id.titlebar_text)
    TextView titlebarText;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.subscribe_ser_name)
    TextView subscribeSerName;
    @BindView(R.id.subscribe_ser_time)
    TextView subscribeSerTime;
    @BindView(R.id.show_num)
    TextView showNum;
    @BindView(R.id.subscribe_num)
    TextView subscribeNum;
    @BindView(R.id.sub_start_time)
    TextView subStartTime;
    @BindView(R.id.sub_start_time_et)
    TextView subStartTimeEt;
    @BindView(R.id.sub_stop_time)
    TextView subStopTime;
    @BindView(R.id.sub_stop_time_et)
    TextView subStopTimeEt;
    @BindView(R.id.sub_ser_person)
    TextView subSerPerson;
    @BindView(R.id.sub_ser_person_et)
    EditText subSerPersonEt;
    @BindView(R.id.checkSystem)
    CheckBox checkSystem;
    @BindView(R.id.sub_ser_remark)
    TextView subSerRemark;
    @BindView(R.id.sub_ser_remark_et)
    EditText subSerRemarkEt;
    @BindView(R.id.subscribe_num1)
    TextView subscribeNum1;
    @BindView(R.id.sub_start_time1)
    TextView subStartTime1;
    @BindView(R.id.sub_start_time_et1)
    TextView subStartTimeEt1;
    @BindView(R.id.sub_stop_time1)
    TextView subStopTime1;
    @BindView(R.id.sub_stop_time_et1)
    TextView subStopTimeEt1;
    @BindView(R.id.sub_ser_person1)
    TextView subSerPerson1;
    @BindView(R.id.sub_ser_person_et1)
    EditText subSerPersonEt1;
    @BindView(R.id.checkSystem1)
    CheckBox checkSystem1;
    @BindView(R.id.sub_ser_remark1)
    TextView subSerRemark1;
    @BindView(R.id.sub_ser_remark_et1)
    EditText subSerRemarkEt1;
    @BindView(R.id.subscribe_num2)
    TextView subscribeNum2;
    @BindView(R.id.sub_start_time2)
    TextView subStartTime2;
    @BindView(R.id.sub_start_time_et2)
    TextView subStartTimeEt2;
    @BindView(R.id.sub_stop_time2)
    TextView subStopTime2;
    @BindView(R.id.sub_stop_time_et2)
    TextView subStopTimeEt2;
    @BindView(R.id.sub_ser_person2)
    TextView subSerPerson2;
    @BindView(R.id.sub_ser_person_et2)
    EditText subSerPersonEt2;
    @BindView(R.id.checkSystem2)
    CheckBox checkSystem2;
    @BindView(R.id.sub_ser_remark2)
    TextView subSerRemark2;
    @BindView(R.id.sub_ser_remark_et2)
    EditText subSerRemarkEt2;
    @BindView(R.id.subscribe_num3)
    TextView subscribeNum3;
    @BindView(R.id.sub_start_time3)
    TextView subStartTime3;
    @BindView(R.id.sub_start_time_et3)
    TextView subStartTimeEt3;
    @BindView(R.id.sub_stop_time3)
    TextView subStopTime3;
    @BindView(R.id.sub_stop_time_et3)
    TextView subStopTimeEt3;
    @BindView(R.id.sub_ser_person3)
    TextView subSerPerson3;
    @BindView(R.id.sub_ser_person_et3)
    EditText subSerPersonEt3;
    @BindView(R.id.checkSystem3)
    CheckBox checkSystem3;
    @BindView(R.id.sub_ser_remark3)
    TextView subSerRemark3;
    @BindView(R.id.sub_ser_remark_et3)
    EditText subSerRemarkEt3;
    @BindView(R.id.subscribe_num4)
    TextView subscribeNum4;
    @BindView(R.id.sub_start_time4)
    TextView subStartTime4;
    @BindView(R.id.sub_start_time_et4)
    TextView subStartTimeEt4;
    @BindView(R.id.sub_stop_time4)
    TextView subStopTime4;
    @BindView(R.id.sub_stop_time_et4)
    TextView subStopTimeEt4;
    @BindView(R.id.sub_ser_person4)
    TextView subSerPerson4;
    @BindView(R.id.sub_ser_person_et4)
    EditText subSerPersonEt4;
    @BindView(R.id.checkSystem4)
    CheckBox checkSystem4;
    @BindView(R.id.sub_ser_remark4)
    TextView subSerRemark4;
    @BindView(R.id.sub_ser_remark_et4)
    EditText subSerRemarkEt4;
    @BindView(R.id.subscribe_query)
    TextView subscribeQuery;
    @BindView(R.id.subscribe_list)
    ListView subscribeList;
    @BindView(R.id.addressTxt)
    TextView addressTxt;

    @BindView(R.id.image_break)
    ImageView imageBreak;
    @BindView(R.id.address_content)
    RelativeLayout addressContent;
    @BindView(R.id.show_address_all)
    CheckableLinearLayout showAddressAll;
    @BindView(R.id.address_select)
    AddListView address_select;
    @BindView(R.id.btn_commint_sub)
    Button btnCommintSub;
    @BindView(R.id.one_time)
    LinearLayout oneTime;
    @BindView(R.id.two_time)
    LinearLayout twoTime;
    @BindView(R.id.three_time)
    LinearLayout threeTime;
    @BindView(R.id.four_time)
    LinearLayout fourTime;
    @BindView(R.id.five_time)
    LinearLayout fiveTime;
    @BindView(R.id.serSystem)
    CheckBox serSystem;
    @BindView(R.id.serSystem1)
    CheckBox serSystem1;
    @BindView(R.id.serSystem2)
    CheckBox serSystem2;
    @BindView(R.id.serSystem3)
    CheckBox serSystem3;
    private String userIdStr, orgId, token, serStockDetailId;
    SerStockDetailWapBean infoData = null;
    String checkSys0 = "1", checkSys1 = "1", checkSys2 = "1", checkSys3 = "1", checkSys4 = "1";
    OldManInfo oldManInfo = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        subscribeSerTime.setText(TimeUtil.getTime() + "");
        showAddressAll.setVisibility(View.GONE);
        token = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.TOKEN_STR, getApplicationContext());
        orgId = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_ID, getApplicationContext());

        view = LayoutInflater.from(LlhSubscribeActivity.this).inflate(R.layout.llh_buttom_view, null);
        add_Address = (TextView) view.findViewById(R.id.add_address2);
        if (getIntent() != null) {
            oldManInfo = (OldManInfo) getIntent().getSerializableExtra("customerId");
            infoData = (SerStockDetailWapBean) getIntent().getSerializableExtra("infoData");
            if (infoData != null && oldManInfo != null) {
                serStockDetailId = infoData.getSerStockDetailId() + "";
                userIdStr = oldManInfo.getUserId();
                subscribeSerName.setText("" + infoData.getServiceName());
                reqServerSelect(userIdStr, subscribeSerTime.getText().toString(), serStockDetailId);
            }
        }
        reqMyAddressList("3", userIdStr);
        checkSystem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkSys0 = "1";
                    subSerPersonEt.setText("");
                } else {
                    checkSys0 = "2";
                }

            }
        });
        checkSystem4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkSys4 = "1";
                    subSerPersonEt3.setText("");
                } else {
                    checkSys4 = "2";
                }
            }
        });
        checkSystem1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkSys1 = "1";
                    subSerPersonEt1.setText("");
                } else {
                    checkSys1 = "2";
                }
            }
        });
        checkSystem2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkSys2 = "1";
                    subSerPersonEt2.setText("");
                } else {
                    checkSys2 = "2";
                }
            }
        });
        checkSystem3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkSys3 = "1";
                    subSerPersonEt3.setText("");
                } else {
                    checkSys3 = "2";
                }
            }
        });
        serSystem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkSys0 = "2";
                } else {
                    checkSys0 = "1";
                }
                checkSystem.setChecked(false);
                showDialog(serdataList, serSystem);
            }
        });
        serSystem1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkSys1 = "2";
                } else {
                    checkSys1 = "1";
                }
                checkSystem1.setChecked(false);
                showDialog(serdataList, serSystem1);
            }
        });
        serSystem2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkSys2 = "2";
                } else {
                    checkSys2 = "1";
                }
                checkSystem2.setChecked(false);
                showDialog(serdataList, serSystem2);
            }
        });
        serSystem3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkSys3 = "2";
                } else {
                    checkSys3 = "1";
                }
                checkSystem3.setChecked(false);
                showDialog(serdataList, serSystem3);
            }
        });
//        setTimeData(subStartTimeEt);
//        setTimeData(subStopTimeEt);
//        setTimeData(subStartTimeEt2);
//        setTimeData(subStopTimeEt2);
//        setTimeData(subStartTimeEt3);
//        setTimeData(subStopTimeEt3);
//        setTimeData(subStartTimeEt4);
//        setTimeData(subStopTimeEt4);
        btnCommintSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(subStartTimeEt1s)) {
                    T.showAnimToast(getApplicationContext(), "请选择服务时间！");
                    return;
                }
                setData(subStartTimeEt1s, subStartTimeEt2s, subStartTimeEt3s, subStartTimeEt4s, subStopTimeEt1s, subStopTimeEt2s, subStopTimeEt3s, subStopTimeEt4s);
                commitServerSelect(setStockDetail(oldManInfo, orderBenaList), serStockDetailId);

            }
        });
        imageBreak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setShowAddress();
            }
        });
    }

    List<SerStockDetailWapBean> orderInfoList = new ArrayList<>();

    private void reqServerSelect(String customerId, String enableDate, String serStockDetailId) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("userId", customerId);
        lRequest.addParam("serStockDetailId", serStockDetailId);
        lRequest.addParam("token", token);
        lRequest.addParam("enableDate", enableDate);
        lRequest.setUri(ServerPort.GET_DETAIL_TYPE);
        lRequest.setUriType(UrlsTools.SERVER_KUC_INFO);
        lRequest.setIp(UrlsTools.SERVER_REQ_URL);
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                try {
                    closeProgress();
                    T.showAnimErrorToast(getApplicationContext(), "请求超时！");
                } catch (Exception E) {

                }
            }

            @Override
            public void onNext(String responseBean, String code) {
                Log.d(TAG, "responseBeansss==" + responseBean.toString());
                try {
                    closeProgress();
                } catch (Exception E) {
                }
                if (responseBean != null && "200".equals(code)) {
                    orderInfoList.clear();
                    orderInfoList = new Gson().fromJson(responseBean,
                            new TypeToken<List<SerStockDetailWapBean>>() {
                            }.getType());
                    DetailListAdapter adapter = new DetailListAdapter(getApplicationContext(),
                            orderInfoList);
                    subscribeList.setAdapter(adapter);
                    adapter.registCallBack(LlhSubscribeActivity.this);
                    adapter.notifyDataSetChanged();
                } else {
                    T.showAnimToast(getApplicationContext(), "没有数据！");
                }
            }
        });
    }

    ServerInfoBean serverInfoBean = null;
    List<ServerInfoBean.DataBean> serdataList = new ArrayList<>();

    private void reqServerInfo(String serviceId, String packDetailId, String orderDateStr, String startTime) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("packDetailId", packDetailId);
        lRequest.addParam("serviceId", serviceId);
        lRequest.addParam("supplierId", orgId);
        lRequest.addParam("orderDateStr", orderDateStr);
        lRequest.addParam("busyState", 1);
        lRequest.addParam("startTime", startTime);
        lRequest.addParam("token", token);
        lRequest.setUri(ServerPort.QUERY_SERVER_INFO);
        lRequest.setReqType(UrlsTools.SERVER_REQ);
        lRequest.setUriType(UrlsTools.SERVER_KUC_INFO);
        lRequest.setIp(UrlsTools.SERVER_REQ_URL);
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                try {
                    closeProgress();
                    T.showAnimErrorToast(getApplicationContext(), "请求超时！");
                } catch (Exception E) {

                }
            }

            @Override
            public void onNext(String responseBean, String code) {
                Log.d(TAG, "responseBeansss==" + responseBean.toString());
                try {
                    closeProgress();
                } catch (Exception E) {
                }
                if (responseBean != null && "0".equals(code)) {
                    serdataList.clear();
                    serverInfoBean = new Gson().fromJson(responseBean, ServerInfoBean.class);
                    if (serverInfoBean != null) {
                        serdataList = serverInfoBean.getData();
                        if (serdataList != null) {
//                            showDialog(serdataList, null);
                        }
                    }
                } else {
                    T.showAnimToast(getApplicationContext(), "没有数据！");
                }
            }
        });
    }

    private void commitServerSelect(String orderData, String serStockDetailId) {
        try {
            LRequest lRequest = new LRequest();
            lRequest.addParam("orderData", URLEncoder.encode(orderData, "utf-8"));
            lRequest.addParam("serStockDetailId", serStockDetailId);
            lRequest.setUri(ServerPort.CONFIM_DETAIL_TYPE);
            lRequest.setUriType(UrlsTools.SERVER_KUC_INFO);
            lRequest.setIp(UrlsTools.SERVER_REQ_URL);
            lRequest.setType(String.class);
            requestData(lRequest, new NetDataListener<String>() {
                @Override
                public void onError(Throwable t) {
                    try {
                        orderBenaList.clear();
                        closeProgress();
                        T.showAnimErrorToast(getApplicationContext(), "请求超时！");
                    } catch (Exception E) {

                    }
                }

                @Override
                public void onNext(String responseBean, String code) {
                    Log.d(TAG, "responseBeansss==" + responseBean.toString());
                    if (responseBean != null && "200".equals(code)) {
                        orderBenaList.clear();
//                        onBackPressed();
                        showInfo();
                    } else {
                        orderBenaList.clear();
                        T.showAnimToast(getApplicationContext(), "预约失败，请检查信息是否完整！");
                    }
                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showProgress() {
        try {
            showProgress(LlhSubscribeActivity.this);
        } catch (Exception e) {

        }

    }

    @Override
    public void hideProgress() {
        try {
            closeProgress();
        } catch (Exception e) {

        }
    }

    @OnClick({R.id.left_btn, R.id.subscribe_query, R.id.subscribe_ser_time, R.id.sub_start_time_et, R.id.sub_stop_time_et, R.id.sub_ser_person_et, R.id.sub_ser_remark_et, R.id.sub_start_time_et1, R.id.sub_stop_time_et1, R.id.sub_ser_person_et1, R.id.sub_ser_remark_et1, R.id.sub_start_time_et2, R.id.sub_stop_time_et2, R.id.sub_ser_person_et2, R.id.sub_ser_remark_et2, R.id.sub_start_time_et3, R.id.sub_stop_time_et3, R.id.sub_ser_person_et3, R.id.sub_ser_remark_et3, R.id.sub_start_time_et4, R.id.sub_stop_time_et4, R.id.sub_ser_person_et4, R.id.sub_ser_remark_et4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_btn:
                hideInputWindow(LlhSubscribeActivity.this);
                onBackPressed();
                break;
            case R.id.subscribe_ser_time:
                TimeShowUtils.showTime(subscribeSerTime, LlhSubscribeActivity.this);
                break;
            case R.id.subscribe_query:
                reqServerSelect(userIdStr, subscribeSerTime.getText().toString(), serStockDetailId);
                break;
            case R.id.sub_start_time_et:
                setTimeData(subStartTimeEt);
                break;
            case R.id.sub_stop_time_et:
                setTimeData(subStopTimeEt);
                break;
            case R.id.sub_ser_person_et:
                break;
            case R.id.sub_ser_remark_et:
                break;
            case R.id.sub_start_time_et1:
                setTimeData(subStartTimeEt1);
                break;
            case R.id.sub_stop_time_et1:
                setTimeData(subStopTimeEt1);
                break;
            case R.id.sub_ser_person_et1:
                break;
            case R.id.sub_ser_remark_et1:
                break;
            case R.id.sub_start_time_et2:
                setTimeData(subStartTimeEt2);
                break;
            case R.id.sub_stop_time_et2:
                setTimeData(subStopTimeEt2);
                break;
            case R.id.sub_ser_person_et2:
                break;
            case R.id.sub_ser_remark_et2:
                break;
            case R.id.sub_start_time_et3:
                setTimeData(subStartTimeEt3);
                break;
            case R.id.sub_stop_time_et3:
                setTimeData(subStopTimeEt3);
                break;
            case R.id.sub_ser_person_et3:
                break;
            case R.id.sub_ser_remark_et3:
                break;
            case R.id.sub_start_time_et4:
                setTimeData(subStartTimeEt4);
                break;
            case R.id.sub_stop_time_et4:
                setTimeData(subStopTimeEt4);
                break;
            case R.id.sub_ser_person_et4:
                break;
            case R.id.sub_ser_remark_et4:
                break;
        }
    }

    public String setStockDetail(OldManInfo oldManInfo, List<OrderBean> orderBean) {
        SubscribeOrderBean subscribeOrder = new SubscribeOrderBean();
        subscribeOrder.setMobile(oldManInfo.getPhone());
        subscribeOrder.setOrderPerson(oldManInfo.getUserName());
        subscribeOrder.setOrderPersonId(oldManInfo.getUserId());
        subscribeOrder.setOrderDateStr(subscribeSerTime.getText().toString());
        subscribeOrder.setRemark("请按时到达");
        subscribeOrder.setOrderList(orderBean);
        subscribeOrder.setCustomerAddress(addressTxt.getText().toString() + "");
        Log.d(TAG, "new Gson()==" + new Gson().toJson(subscribeOrder).toString());
        return new Gson().toJson(subscribeOrder);
    }


    /**
     * 设置预约
     */
    private void setData(String subStartTimeEt1s, String subStartTimeEt2s, String subStartTimeEt3s, String subStartTimeEt4s, String subStopTimeEt1s, String subStopTimeEt2s, String subStopTimeEt3s, String subStopTimeEt4s) {
        if (getExecNumDay == 1) {
            Log.d(TAG, "subStartTimeEt1s==" + subStartTimeEt1s + "subStopTimeEt1s==" + subStopTimeEt1s);
            setOrderBenaData(subStartTimeEt1s, subStopTimeEt1s, serUserId, serUserName, checkSys0, 1 + "");
        } else if (getExecNumDay == 2) {
            setOrderBenaData(subStartTimeEt1s, subStopTimeEt1s, serUserId, serUserName, checkSys0, 1 + "");
            setOrderBenaData(subStartTimeEt2s, subStopTimeEt2s, serUserId1, serUserName1, checkSys2, 2 + "");
        } else if (getExecNumDay == 3) {
            setOrderBenaData(subStartTimeEt1s, subStopTimeEt1s, serUserId, serUserName, checkSys0, 1 + "");
            setOrderBenaData(subStartTimeEt2s, subStopTimeEt2s, serUserId1, serUserName1, checkSys2, 2 + "");
            setOrderBenaData(subStartTimeEt3s, subStopTimeEt3s, serUserId2, serUserName2, checkSys3, 3 + "");
        } else if (getExecNumDay == 4) {
            setOrderBenaData(subStartTimeEt1s, subStopTimeEt1s, serUserId, serUserName, checkSys0, 1 + "");
            setOrderBenaData(subStartTimeEt2s, subStopTimeEt2s, serUserId1, serUserName1, checkSys2, 2 + "");
            setOrderBenaData(subStartTimeEt3s, subStopTimeEt3s, serUserId2, serUserName2, checkSys3, 3 + "");
            setOrderBenaData(subStartTimeEt4s, subStopTimeEt4s, serUserId3, serUserName3, checkSys4, 4 + "");
        }
    }

    @OnClick(R.id.image_break)
    public void onViewClicked() {
        setShowAddress();
    }

    List<OrderBean> orderBenaList = new ArrayList<>();

    public void setOrderBenaData(String startTime, String stopTime, String serUserId, String serUserName, String isChange, String execNum) {
        OrderBean orderBena = new OrderBean();
        orderBena.setStartTime(startTime);
        if (TextUtils.isEmpty(serUserId) && TextUtils.isEmpty(serUserName)) {
            orderBena.setWaiters("");
        } else {
            orderBena.setWaiters(serUserId + "_" + serUserName);
        }
        if (stopTime == null || "".equals(stopTime)) {
            orderBena.setEndTime("");
        } else {
            orderBena.setEndTime(stopTime);
        }
        orderBena.setIsChange(isChange);
        orderBena.setAssignType(Integer.valueOf(isChange));
        orderBena.setOrdreNum(execNum);
        orderBenaList.add(orderBena);
    }


    @Override
    protected int getlayout() {
        return R.layout.llh_subscribe_activity;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void doBasess() {

    }

    @Override
    protected INetRequestManager getRequestManager() {
        return ServerNetRequestManager.getInstance();
    }

    int getExecNumDay;
    private SerStockDetailWapBean serStockDetailWapBeanData = null;

    public void callBack(SerStockDetailWapBean serStockDetailWapBean) {
        if (serStockDetailWapBean != null) {
            serStockDetailWapBeanData = serStockDetailWapBean;
            showAddressAll.setVisibility(View.VISIBLE);
            if (serStockDetailWapBean.getCustomerAddress() == null) {
                setShowAddress(0);
            } else {
                addressTxt.setText("" + serStockDetailWapBean.getCustomerAddress());
            }
            getExecNumDay = serStockDetailWapBean.getExecNumDay();
            showNum.setText("每日执行" + serStockDetailWapBean.getExecNumDay() + "次");
            if (serStockDetailWapBean.getExecNumDay() == 1) {
                subscribeNum.setText("请完成以下预约条件：");
                Log.d(TAG, "getExecTimeDayA==" + serStockDetailWapBean.getExecNumDay());
                oneTime.setVisibility(View.VISIBLE);
                btnCommintSub.setVisibility(View.VISIBLE);
            } else if (serStockDetailWapBean.getExecNumDay() == 2) {
                Log.d(TAG, "getExecTimeDayB==" + serStockDetailWapBean.getExecNumDay());
                oneTime.setVisibility(View.VISIBLE);
                twoTime.setVisibility(View.VISIBLE);
                btnCommintSub.setVisibility(View.VISIBLE);
            } else if (serStockDetailWapBean.getExecNumDay() == 3) {
                Log.d(TAG, "getExecTimeDayC==" + serStockDetailWapBean.getExecNumDay());
                oneTime.setVisibility(View.VISIBLE);
                twoTime.setVisibility(View.VISIBLE);
                threeTime.setVisibility(View.VISIBLE);
                btnCommintSub.setVisibility(View.VISIBLE);
            } else if (serStockDetailWapBean.getExecNumDay() == 4) {
                Log.d(TAG, "getExecTimeDayD==" + serStockDetailWapBean.getExecNumDay());
                oneTime.setVisibility(View.VISIBLE);
                twoTime.setVisibility(View.VISIBLE);
                threeTime.setVisibility(View.VISIBLE);
                fourTime.setVisibility(View.VISIBLE);
                btnCommintSub.setVisibility(View.VISIBLE);
            } else if (serStockDetailWapBean.getExecNumDay() == 5) {
                oneTime.setVisibility(View.VISIBLE);
                twoTime.setVisibility(View.VISIBLE);
                threeTime.setVisibility(View.VISIBLE);
                fourTime.setVisibility(View.VISIBLE);
                fiveTime.setVisibility(View.VISIBLE);
                btnCommintSub.setVisibility(View.VISIBLE);
            }
//            reqServerInfo(serStockDetailWapBean.getServiceId(), serStockDetailWapBean.getPackDetailId() + "",subscribeSerTime.getText().toString(),);
        }
    }

    private Dialog builder = null;
    private ListView shardDvList;
    ImageView cloase;
    private ServerInfoBean.DataBean serDataBean = null;
    String serUserName, serUserId, serUserName1, serUserId1, serUserName2, serUserId2, serUserName3, serUserId3, serUserName4, serUserId4;

    //选择服务员
    public void showDialog(final List<ServerInfoBean.DataBean> mySerList, final CheckBox checkBox) {
        Log.d(TAG, "mySerList.size===" + mySerList.size());
        if (mySerList != null && mySerList.size() == 0) {
            ToastTool.showText(getApplicationContext(), "抱歉，暂时没有服务员相关信息，请选择系统指派！");
            return;
        }
        builder = new Dialog(this);
        builder.setTitle("请选择服务员");
        builder.show();
        LayoutInflater inflater = LayoutInflater.from(this);
        View viewDialog = inflater.inflate(R.layout.shard_videcode_list, null);
        Display display = this.getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        //设置对话框的宽高
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width * 90 / 100, ViewGroup.LayoutParams.WRAP_CONTENT);
        builder.setContentView(viewDialog, layoutParams);
        cloase = (ImageView) viewDialog.findViewById(R.id.cloase);
        shardDvList = (ListView) viewDialog.findViewById(R.id.shard_device_list);
        ServerInfoAdapter serverInfoAdapter = new ServerInfoAdapter(getApplicationContext(), mySerList);
        shardDvList.setAdapter(serverInfoAdapter);
        shardDvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                serDataBean = mySerList.get(position);
                if (serDataBean != null) {
                    if (checkBox == serSystem) {
                        subSerPersonEt.setText("" + serDataBean.getUserName());
                        serUserName = serDataBean.getUserName() + "_" + serDataBean.getCurrentOrgId() + "_" + serDataBean.getCurrentOrgName();
                        serUserId = serDataBean.getUserId();
                    } else if (checkBox == serSystem1) {
                        subSerPersonEt1.setText("" + serDataBean.getUserName());
                        serUserName1 = serDataBean.getUserName() + "_" + serDataBean.getCurrentOrgId() + "_" + serDataBean.getCurrentOrgName();
                        serUserId1 = serDataBean.getUserId();
                    } else if (checkBox == serSystem2) {
                        subSerPersonEt2.setText("" + serDataBean.getUserName());
                        serUserName2 = serDataBean.getUserName() + "_" + serDataBean.getCurrentOrgId() + "_" + serDataBean.getCurrentOrgName();
                        serUserId2 = serDataBean.getUserId();
                    } else if (checkBox == serSystem3) {
                        subSerPersonEt3.setText("" + serDataBean.getUserName());
                        serUserName3 = serDataBean.getUserName() + "_" + serDataBean.getCurrentOrgId() + "_" + serDataBean.getCurrentOrgName();
                        serUserId3 = serDataBean.getUserId();
                    }
                }
                if (builder != null && builder.isShowing()) {
                    builder.dismiss();
                }
            }
        });
        cloase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (builder != null && builder.isShowing()) {
                    builder.dismiss();
                }
            }
        });
    }

    private void setTimeData(final TextView text) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = View.inflate(this, R.layout.time_dialog, null);
        final TimePicker timePicker = (TimePicker) view
                .findViewById(R.id.time_picker);
        builder.setView(view);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        timePicker.setIs24HourView(true);

        timePicker.setCurrentHour(cal.get(Calendar.HOUR_OF_DAY));
        timePicker.setCurrentMinute(Calendar.MINUTE);
        timePicker.setBackgroundColor(Color.GRAY);
        builder.setTitle("请选取时间");
        builder.setPositiveButton("确  定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String time = ((timePicker.getCurrentHour() < 10) ? "0"
                                + timePicker.getCurrentHour() : timePicker
                                .getCurrentHour()) + ":" + ((timePicker.getCurrentMinute() < 10) ? "0"
                                + timePicker.getCurrentMinute() : timePicker.getCurrentMinute());
                        text.setText(time);
                        if (text == subStartTimeEt && !TextUtils.isEmpty(subStartTimeEt.getText().toString())) {
                            subStartTimeEt1s = subStartTimeEt.getText().toString();
                            reqServerInfo(serStockDetailWapBeanData.getServiceId(), serStockDetailWapBeanData.getPackDetailId() + "", subscribeSerTime.getText().toString(), subStartTimeEt1s);
                            Log.d(TAG, "subStartTimeEt1s==" + subStartTimeEt1s);
                        } else if (text == subStopTimeEt && !TextUtils.isEmpty(subStopTimeEt.getText().toString())) {
                            subStopTimeEt1s = subStopTimeEt.getText().toString();
                        } else if (text == subStartTimeEt1 && !TextUtils.isEmpty(subStartTimeEt1.getText().toString())) {
                            subStartTimeEt2s = subStartTimeEt1.getText().toString();
                            Log.d(TAG, "subStartTimeEt2s==" + subStartTimeEt1s);
                            reqServerInfo(serStockDetailWapBeanData.getServiceId(), serStockDetailWapBeanData.getPackDetailId() + "", subscribeSerTime.getText().toString(), subStartTimeEt2s);
                        } else if (text == subStartTimeEt2 && !TextUtils.isEmpty(subStartTimeEt2.getText().toString())) {
                            subStartTimeEt3s = subStartTimeEt2.getText().toString();
                            Log.d(TAG, "subStartTimeEt3s==" + subStartTimeEt1s);
                            reqServerInfo(serStockDetailWapBeanData.getServiceId(), serStockDetailWapBeanData.getPackDetailId() + "", subscribeSerTime.getText().toString(), subStartTimeEt3s);
                        } else if (text == subStartTimeEt3 && !TextUtils.isEmpty(subStartTimeEt3.getText().toString())) {
                            subStartTimeEt4s = subStartTimeEt3.getText().toString();
                            Log.d(TAG, "subStartTimeEt4s==" + subStartTimeEt1s);
                            reqServerInfo(serStockDetailWapBeanData.getServiceId(), serStockDetailWapBeanData.getPackDetailId() + "", subscribeSerTime.getText().toString(), subStartTimeEt4s);
                        } else if (text == subStopTimeEt1 && !TextUtils.isEmpty(subStopTimeEt1.getText().toString())) {
                            subStopTimeEt2s = subStopTimeEt1.getText().toString();
                        } else if (text == subStopTimeEt2 && !TextUtils.isEmpty(subStopTimeEt2.getText().toString())) {
                            subStopTimeEt3s = subStopTimeEt2.getText().toString();
                        } else if (text == subStopTimeEt3 && !TextUtils.isEmpty(subStopTimeEt3.getText().toString())) {
                            subStopTimeEt4s = subStopTimeEt3.getText().toString();
                        }
                        dialog.cancel();
                    }
                });
        Dialog dialog = builder.create();
        dialog.show();
    }

    String subStartTimeEt1s, subStartTimeEt2s, subStartTimeEt3s, subStartTimeEt4s, subStopTimeEt1s, subStopTimeEt2s, subStopTimeEt3s, subStopTimeEt4s;
    AddressManagerBean addressManagerBean = null;
    List<AddressManagerBean.DataBean> dataBeanList = new ArrayList<AddressManagerBean.DataBean>();

    private void reqMyAddressList(String code, String userId) {
        Log.d(TAG, "获取我的地址。。。。");
        LRequest lRequest = new LRequest();
        lRequest.addParam("code", code);
        lRequest.addParam("userId", userId);
        lRequest.addParam("addressType", 1);
        lRequest.addParam("token", token);
        lRequest.setUri(ServerPort.GET_MY_ADDRESS);
        lRequest.setIp(UrlsTools.REQ_URL);
        lRequest.setReqType(UrlsTools.ADDRESS_REQ_URL);
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                T.showAnimErrorToast(LlhSubscribeActivity.this, "请求超时！");
            }

            @Override
            public void onNext(String responseBean, String code) {
                Log.d(TAG, "responseBean==" + responseBean);
                if (responseBean != null && "0".equals(code)) {
                    dataBeanList.clear();
                    addressManagerBean = new Gson().fromJson(responseBean.toString(), AddressManagerBean.class);
                    dataBeanList = addressManagerBean.getData();
                    if (dataBeanList != null) {

                        setAddressData(dataBeanList);
                        for (int i = 0; i < dataBeanList.size(); i++) {
                            if ("1".equals(dataBeanList.get(i).getAddressDefault())) {
                                setShowAddress(i);
                                return;
                            }
                        }
                    } else {
                        T.showAnimToast(getApplicationContext(), "地址信息为空！");
                    }
                }
            }
        });
    }

    View view;
    private TextView add_Address;
    LlhAddressItemAdapter llhAddressItemAdapter = null;
    List<MyAddress> objects = new ArrayList<MyAddress>();

    private void setAddressData(List<AddressManagerBean.DataBean> dataBeanList) {
        objects = new ArrayList<MyAddress>();
        for (int i = 0; i < dataBeanList.size(); i++) {
            MyAddress myAddress = new MyAddress();
            myAddress.setUserName(dataBeanList.get(i).getContactName());
            myAddress.setAddressPhone(dataBeanList.get(i).getContactPhone());
            myAddress.setAddress(dataBeanList.get(i).getAddressAll());
            myAddress.setAddressId(dataBeanList.get(i).getAddressId() + "");
            objects.add(myAddress);
        }
        if (llhAddressItemAdapter == null) {
            llhAddressItemAdapter = new LlhAddressItemAdapter(getApplicationContext(), objects);
            address_select.setAdapter(llhAddressItemAdapter);
            address_select.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            ListViewHeightUtil.setListViewHeightBasedOnChildren(address_select);
            address_select.setVisibility(View.GONE);


        } else {
            llhAddressItemAdapter.setObjects(objects);
            llhAddressItemAdapter.notifyDataSetChanged();
        }
        address_select.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (ListView.CHOICE_MODE_SINGLE == address_select.getChoiceMode()) {
                    setShowAddress(position);
                    setShowAddress();
                }
            }
        });
        add_Address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(getApplicationContext(), LlhAddAddressActivity.class);
//                intent.putExtra("intentType", "1");
//                intent.putExtra("code", "0");
//                startActivity(intent);
            }
        });
    }

    private void setShowAddress() {
        if (objects != null && address_select.getVisibility() == View.GONE) {
            imageBreak.setVisibility(View.VISIBLE);
            address_select.setVisibility(View.VISIBLE);
        } else if (address_select.getVisibility() == View.VISIBLE) {
            address_select.setVisibility(View.GONE);
        }
    }

    private String addressName, addressPhone, addressAll;

    private void setShowAddress(int i) {
        addressName = dataBeanList.get(i).getContactName();
        addressPhone = dataBeanList.get(i).getContactPhone();
        addressAll = dataBeanList.get(i).getAddressAll();
        addressTxt.setText("" + addressAll);
    }


    private void showInfo() {
        final com.server.llh.llhserverwork.view.AlertDialog.Builder builder = new com.server.llh.llhserverwork.view.AlertDialog.Builder(this);
        builder.setTitle("预约成功！");
        builder.setMessage("\n" + "服务已预约成功，祝你生活愉快！" + "\n");

        builder.setNegativeButton("确认退出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                intent.setAction(Constants.BroadCastAction.FINISH_ALL_ACTIVITY);
                sendBroadcast(intent);
                onBackPressed();
                finish();
                dialog.cancel();
            }
        });
        builder.setPositiveButton("继续预约", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
                dialog.cancel();
            }
        });
        builder.show();
    }

}
